﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RhythmGame
{
    public partial class JudgeLine : UserControl
    {
        private static List<Panel> trackLine = new List<Panel>();
        private static List<Panel> tracksDividingLine = new List<Panel>();
        private static Panel tracksLeftEdge = new Panel();
        private static Panel tracksRightEdge = new Panel();

        public JudgeLine()
        {
            InitializeComponent();
            this.SetStyle(ControlStyles.Selectable, true);
            this.TabStop = true;
        }

        private static void InitializeSegments(int quantity)
        {
            int segmentWidth = (int)(Settings.Instance.WinWidth * 0.9) / quantity;
            
            for (int i = 0; i < quantity; i++)
            {
                Panel segment = new Panel
                {
                    Width = segmentWidth,
                    Height = Settings.Instance.JudgeLineSize,
                    BackColor = Settings.Instance.JudgeLineColor,
                    Location = new Point(0, 0)
                };
                trackLine.Add(segment);
            }

            for (int i = 0; i < quantity - 1; i++)
            {
                Panel dividingLine = new Panel
                {
                    Width = 1,
                    Height = Settings.Instance.JudgeHeight - (int)(Settings.Instance.WinHeight * 0.05),
                    BackColor = Color.Gray,
                    Location = new Point(0, 0)
                };
                tracksDividingLine.Add(dividingLine);
            }

            tracksLeftEdge.Width = 1;
            tracksLeftEdge.Height = Settings.Instance.JudgeHeight - (int)(Settings.Instance.WinHeight * 0.05);
            tracksLeftEdge.BackColor = Color.Gray;
            tracksLeftEdge.Location = new Point(0, 0);
            tracksRightEdge.Width = 1;
            tracksRightEdge.Height = Settings.Instance.JudgeHeight - (int)(Settings.Instance.WinHeight * 0.05);
            tracksRightEdge.BackColor = Color.Gray;
            tracksRightEdge.Location = new Point(0, 0);
        }

        private static void JudgeLine_KeyDown(object sender, KeyEventArgs e)
        {
            for (int i = 0; i < Settings.Instance.MaxKeysQuantity; i++)
            {
                if (e.KeyCode == Settings.Instance.KeysLocate[i])
                {
                    trackLine[i].BackColor = Settings.Instance.StrikeColor;
                }
            }
        }

        private static void JudgeLine_KeyUp(object sender, KeyEventArgs e)
        {
            for (int i = 0; i < Settings.Instance.MaxKeysQuantity; i++)
            {
                if (e.KeyCode == Settings.Instance.KeysLocate[i])
                {
                    trackLine[i].BackColor = Settings.Instance.JudgeLineColor;
                }
            }
        }

        internal static void ShowLine(Game game)
        {
            JudgeLine judgeLine = new JudgeLine();
            judgeLine.Location = new Point(0, 0);
            InitializeSegments(Settings.Instance.MaxKeysQuantity);
            judgeLine.Width = Settings.Instance.WinWidth;
            judgeLine.Height = Settings.Instance.JudgeLineSize;
            judgeLine.BackColor = Settings.Instance.JudgeLineColor;
            judgeLine.Top = Settings.Instance.JudgeHeight;
            game.Controls.Add(judgeLine);
            judgeLine.BringToFront();
            for (int i = 0; i < Settings.Instance.MaxKeysQuantity; i++)
            {
                trackLine[i].Top = Settings.Instance.JudgeHeight;
                trackLine[i].Left = (int)(Settings.Instance.WinWidth * 0.05 + trackLine[i].Width * i);
                game.Controls.Add(trackLine[i]);
                trackLine[i].BringToFront();
            }
            

            if (Settings.Instance.SetTracksDividingLineVisible) // 是否显示轨道分割线
            {    
                for (int i = 0; i < Settings.Instance.MaxKeysQuantity - 1; i++)
                {
                    tracksDividingLine[i].Top = (int)(Settings.Instance.WinHeight * 0.05);
                    tracksDividingLine[i].Left = (int)(Settings.Instance.WinWidth * 0.05 + trackLine[i].Width * (i + 1));
                    game.Controls.Add(tracksDividingLine[i]);
                    tracksDividingLine[i].BringToFront();
                }
                tracksLeftEdge.Top = (int)(Settings.Instance.WinHeight * 0.05);
                tracksLeftEdge.Left = (int)(Settings.Instance.WinWidth * 0.05);
                game.Controls.Add(tracksLeftEdge);
                tracksLeftEdge.BringToFront();
                tracksRightEdge.Top = (int)(Settings.Instance.WinHeight * 0.05);
                tracksRightEdge.Left = (int)(Settings.Instance.WinWidth * 0.95);
                game.Controls.Add(tracksRightEdge);
                tracksRightEdge.BringToFront();
            }
            

            // 绑定键盘事件处理程序
            game.KeyDown += JudgeLine_KeyDown;
            game.KeyUp += JudgeLine_KeyUp;

            //judgeLine.Focus(); // 确保 JudgeLine 可以接收键盘事件
        }
    }
}
